/******************************************************************************

	COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2010.

	This file is part of Maxsi Engine.

	Maxsi Engine is free software: you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your
	option) any later version.

	Maxsi engine is distributed in the hope that it will be useful, but WITHOUT
	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
	FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
	for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with Maxsi Engine. If not, see <http://www.gnu.org/licenses/>.

	MaxsiEngine.dll
	A general purpose C++ library for the Maxsi Technology Project.

	MaxsiIODeviceStdOut.cpp
	Outputs all incoming data to the standard output.

******************************************************************************/

#include "MaxsiEngine.h"

#ifdef Maxsi_Support_MaxsiIO

MaxsiIODeviceStdOut::MaxsiIODeviceStdOut()
{
	
}

MaxsiIODeviceStdOut::~MaxsiIODeviceStdOut()
{
	
}

FLAGSL32 MaxsiIODeviceStdOut::GetCapabilities()
{
	return ( MAXSI_IO_SUPPORTS_VIRTUAL_SOCKETS | MAXSI_IO_SUPPORTS_NON_BLOCKING );
}

MaxsiError MaxsiIODeviceStdOut::HandleOpenDevice(MaxsiIOEntity* Remote, MaxsiIOSocket* Socket, Word_t Direction)
{
	if ( Remote == NULL || Socket == NULL ) { return MAXSI_ERROR_INVALID_INPUT; }

	if ( Direction != MAXSI_IO_DIRECTION_WRITE ) { return MAXSI_ERROR_NOT_SUPPORTED; }

	MaxsiError		Result			=	MAXSI_ERROR_SUCCESS;

	// Create a handle.
	MaxsiIOHandle*	Connection		=	new MaxsiIOHandle;

	// Check if the handle was created.
	Result	=	( Connection == NULL ) ? MAXSI_ERROR_MEM_ALLOC_FAIL : Result;
	
	if ( Result == MAXSI_ERROR_SUCCESS )
	{
		// Reset the connection handle.
		memset(Connection, 0, sizeof(MaxsiIOHandle));

		// Set the variables.
		Connection->Device		=	this;
		Connection->Connection	=	Remote;
		Connection->Socket		=	Socket;
		Connection->Direction	=	MAXSI_IO_DIRECTION_WRITE;
	}

	if ( Result == MAXSI_ERROR_SUCCESS ) { delete Connection; return NULL; }
	if ( Result != MAXSI_ERROR_SUCCESS ) { *Socket = Connection; } 

	return Result;
}

MaxsiError MaxsiIODeviceStdOut::AttemptRead(MaxsiIOHandle* Connection, BYTE* Buffer, size_t* BufferSize, size_t* Transferred)
{
	return MAXSI_ERROR_NOT_SUPPORTED;
}

MaxsiError MaxsiIODeviceStdOut::AttemptWrite(MaxsiIOHandle* Connection, BYTE* Buffer, size_t* BufferSize, size_t* Transferred)
{
	if ( Connection == NULL || Buffer == NULL || BufferSize == NULL || Transferred == NULL ) { return MAXSI_ERROR_INVALID_INPUT; }

	if ( *BufferSize == 0 ) { return MAXSI_ERROR_SUCCESS; }

	char LastChar = Buffer[*BufferSize-1];

	Buffer[*BufferSize-1] = 0;

	printf("%s%c", (char*)Buffer, LastChar);

	Buffer[*BufferSize-1] = LastChar;

	*Transferred = *BufferSize;

	return MAXSI_ERROR_SUCCESS;
}

MaxsiError MaxsiIODeviceStdOut::HandleDisconnect(MaxsiIOSocket* Socket)
{
	return MAXSI_ERROR_SUCCESS;
}

#endif

